//
//  YBBaseNavigationController.m
//  YBInsurance
//
//  Created by S2 on 12/15/15.
//  Copyright © 2015 YB. All rights reserved.
//

#import "YBBaseNavigationController.h"

@interface YBBaseNavigationController () <UINavigationControllerDelegate, UIGestureRecognizerDelegate>

@end

@implementation YBBaseNavigationController

- (void)dealloc
{
    self.interactivePopGestureRecognizer.delegate = nil;
    self.delegate = nil;
}

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController {
    self = [super initWithRootViewController:rootViewController];
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    __weak typeof(&*self) weakSelf = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = weakSelf;
        self.delegate = weakSelf;
    }
}

#pragma mark - override

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
//    viewController.hidesBottomBarWhenPushed = YES;
        [super pushViewController:viewController animated:animated];
}

#pragma mark - UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        
        if (navigationController.viewControllers.count == 1) {
            
            navigationController.interactivePopGestureRecognizer.enabled = NO;
            navigationController.interactivePopGestureRecognizer.delegate = nil;
        } else {
            
            navigationController.interactivePopGestureRecognizer.enabled = YES;
        }
    }
}


@end
